﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System.Web;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// PLM物料编码定时解码
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PLMMaterialDecodController : ControllerBase
    {
        private readonly ILogger<PLMMaterialDecodRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_wms_test;


        public PLMMaterialDecodController(ILogger<PLMMaterialDecodRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_wms_test= _configuration.GetConnectionString("WMSDbConnection_Test");

        }

        /// <summary>
        /// PLM物料编码定时解码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostPLMMaterialDecod([FromBody] PLMMaterialDecodRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            PLMMaterialDecodResponseDto re = new PLMMaterialDecodResponseDto();
            


            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("material", "");//查看URL配置表地址描述
                DataTable dt_material = MethodHelp.GetOutsideTableInfo("TMS0016", dic, con_wms_test, con);//获取需要解码的物料编码
                if (dt_material.Rows.Count>0)
                {
                    foreach (DataRow item in dt_material.Rows)
                    {
                        string material_code = item["material_code"].ToString();
                        string material_name = "";
                        string make_type = "";
                        string stick_model = "";
                        string stick_type = "";
                        string stick_size = "";
                        string material_thickness = "";
                        string stick_grade = "";
                        string material_grade = "";
                        string material_resistance = "";
                        string material_lifetime = "";
                        string material_aspect = "";
                        string material_diagonal = "";
                        string material_projection = "";
                        string material_chamfer = "";
                        string factory_code = "C01";

                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("decode_level", "第二级");
                        dic.Add("material_code", material_code[2].ToString()+ material_code[3].ToString());
                        DataTable dt_jm2 = MethodHelp.GetTableInfo("TMS0018", dic, con);//获取第二级解码
                        if (dt_jm2.Rows.Count>0)
                        {
                            stick_model = dt_jm2.Rows[0]["material_name"].ToString()[0].ToString()+ "型";
                            stick_grade = dt_jm2.Rows[0]["material_name"].ToString().Substring(2)+ "级";

                            dic = new Dictionary<string, string>(); //查询公用dic
                            dic.Add("decode_level", "第四级");
                            dic.Add("material_code", material_code[6].ToString() + material_code[7].ToString());
                            DataTable dt_jm4 = MethodHelp.GetTableInfo("TMS0018", dic, con);//获取第四级解码
                            if (dt_jm4.Rows.Count>0)
                            {
                                stick_type = dt_jm2.Rows[0]["expand_fields"].ToString()+ dt_jm4.Rows[0]["expand_fields"].ToString();
                                material_aspect = dt_jm4.Rows[0]["material_name"].ToString().Substring(0, dt_jm4.Rows[0]["material_name"].ToString().IndexOf("*"));
                                material_diagonal = dt_jm4.Rows[0]["material_name"].ToString().Substring(dt_jm4.Rows[0]["material_name"].ToString().IndexOf("*")+1);

                                dic = new Dictionary<string, string>(); //查询公用dic
                                dic.Add("decode_level", "第五级");
                                dic.Add("material_code", material_code[8].ToString() + material_code[9].ToString());
                                DataTable dt_jm5 = MethodHelp.GetTableInfo("TMS0018", dic, con);//获取第五级解码
                                if (dt_jm5.Rows.Count>0)
                                {
                                    material_thickness = dt_jm5.Rows[0]["material_name"].ToString();

                                    dic = new Dictionary<string, string>(); //查询公用dic
                                    dic.Add("decode_level", "第六级");
                                    dic.Add("material_code", material_code[10].ToString() + material_code[11].ToString());
                                    DataTable dt_jm6 = MethodHelp.GetTableInfo("TMS0018", dic, con);//获取第五级解码
                                    if (dt_jm5.Rows.Count > 0)
                                    {
                                        material_grade= dt_jm6.Rows[0]["material_name"].ToString();

                                        dic = new Dictionary<string, string>(); //查询公用dic
                                        dic.Add("decode_level", "第三级");
                                        dic.Add("material_code", material_code[4].ToString() + material_code[5].ToString());
                                        DataTable dt_jm3 = MethodHelp.GetTableInfo("TMS0018", dic, con);//获取第五级解码
                                        if (dt_jm3.Rows.Count>0)
                                        {
                                            material_resistance= dt_jm3.Rows[0]["material_name"].ToString();

                                            dic = new Dictionary<string, string>(); //查询公用dic
                                            dic.Add("decode_level", "第七级");
                                            dic.Add("material_code", material_code[12].ToString());
                                            DataTable dt_jm7 = MethodHelp.GetTableInfo("TMS0018", dic, con);//获取第五级解码
                                            if (dt_jm7.Rows.Count>0)
                                            {
                                                material_chamfer = dt_jm7.Rows[0]["material_name"].ToString();

                                                dic = new Dictionary<string, string>(); //查询公用dic
                                                dic.Add("decode_level", "第八级");
                                                dic.Add("material_code", material_code[13].ToString() + material_code[14].ToString());
                                                DataTable dt_jm8 = MethodHelp.GetTableInfo("TMS0018", dic, con);//获取第五级解码
                                                if (dt_jm8.Rows.Count>0)
                                                {
                                                    make_type = dt_jm8.Rows[0]["material_name"].ToString();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                           
                        }

                        dic = new Dictionary<string, string>();
                        dic.Add("material_code", material_code);
                        dic.Add("material_name", material_name);
                        dic.Add("make_type", make_type);
                        dic.Add("stick_size", stick_size);
                        dic.Add("stick_model", stick_model);
                        dic.Add("stick_type", stick_type);
                        dic.Add("material_thickness", material_thickness);
                        dic.Add("stick_grade", stick_grade);
                        dic.Add("material_grade", material_grade);
                        dic.Add("material_resistance", material_resistance);
                        dic.Add("material_lifetime", material_lifetime);
                        dic.Add("material_aspect", material_aspect);
                        dic.Add("material_diagonal", material_diagonal);
                        dic.Add("material_projection", material_projection);
                        dic.Add("material_chamfer", material_chamfer);
                        dic.Add("factory_code", factory_code);
                        strMsg = MethodHelp.InsertIntoTable("TMS0017", dic,con);
                    }
                }


            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.code = 0;
               
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
